if not Skada then return end
local Skada = Skada

local mod = Skada:NewModule("Lightwell")
local spellsmod = Skada:NewModule("Lightwell spell list")

local function log_lw_heal(set, heal)
	-- Get the player from set.
	local player = Skada:get_player(set, heal.playerid, heal.playername)
	if player then
		-- Subtract overhealing
		local amount = math.max(0, heal.amount - heal.overhealing)

		-- Add to player total.
		player.lwhealing = player.lwhealing + amount
		player.lwoverhealing = player.lwoverhealing + heal.overhealing
		
		-- Also add to set total healing.
		set.lwhealing = set.lwhealing + amount
		
		-- Create spell if it does not exist.
		if not player.lwhealingspells[heal.spellname] then
			player.lwhealingspells[heal.spellname] = {id = heal.spellid, name = heal.spellname, hits = 0, healing = 0, overhealing = 0, min = 0, max = 0}
		end
		
		-- Get the spell from player.
		local spell = player.lwhealingspells[heal.spellname]
		
		spell.healing = spell.healing + amount
		spell.overhealing = spell.overhealing + heal.overhealing
		spell.hits = (spell.hits or 0) + 1
		
		if not spell.min or amount < spell.min then
			spell.min = amount
		end
		if not spell.max or amount > spell.max then
			spell.max = amount
		end
	end
end

local heal = {}
local function LWSpellHeal(timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, ...)
	local spellID, spellName, spellSchool, samount, soverhealing, absorbed, scritical = ...
	
	-- Lightwells -- Healtstone
	if spellID == 7001 or spellID == 6262 then
		heal.spellid = spellID
		heal.spellname = spellName
		heal.playerid = dstGUID
		heal.dstName = dstName
		heal.amount = samount
		heal.overhealing = soverhealing
		log_lw_heal(Skada.current, heal)
		log_lw_heal(Skada.total, heal)
	end
end

function mod:Update(win, set)
	local nr = 1
	local max = 0

	for i, player in ipairs(set.players) do
		if player.lwhealing > 0 then
			
			local d = win.dataset[nr] or {}
			win.dataset[nr] = d
			
			d.id = player.id
			d.label = player.name
			d.value = player.lwhealing
			
			d.valuetext = Skada:FormatValueText(
											Skada:FormatNumber(player.lwhealing), self.metadata.columns.Healing,
											string.format("%02.1f%%", player.lwoverhealing / (player.lwoverhealing + player.lwhealing) * 100), self.metadata.columns.Overhealing,
											string.format("%02.1f%%", player.lwhealing / set.lwhealing * 100), self.metadata.columns.Percent
										)
			d.class = player.class
			
			if player.lwhealing > max then
				max = player.lwhealing
			end
			
			nr = nr + 1
		end
	end
	
	win.metadata.maxvalue = max
end

local function spell_tooltip(win, id, label, tooltip)
	local player = Skada:find_player(win:get_selected_set(), spellsmod.playerid)
	if player then
		local spell = player. healingspells[label]
		if spell then
			tooltip:AddLine(player.name.." - "..label)
			if spell.max and spell.min then
				tooltip:AddDoubleLine("Minimum hit:", Skada:FormatNumber(spell.min), 255,255,255,255,255,255)
				tooltip:AddDoubleLine("Maximum hit:", Skada:FormatNumber(spell.max), 255,255,255,255,255,255)
			end
			tooltip:AddDoubleLine("Average hit:", Skada:FormatNumber(spell.healing / spell.hits), 255,255,255,255,255,255)
			if spell.hits then
				tooltip:AddDoubleLine("Overhealing"..":", ("%02.1f%%"):format(spell.overhealing / (spell.overhealing + spell.healing) * 100), 255,255,255,255,255,255)
			end
		end
	end
end

function spellsmod:Enter(win, id, label)
	spellsmod.playerid = id
	spellsmod.title = label.."'s LW Healing"
end

-- Spell view of a player.
function spellsmod:Update(win, set)
	-- View spells for this player.
		
	local player = Skada:find_player(set, self.playerid)
	local nr = 1
	local max = 0
	
	if player then
		
		for spellname, spell in pairs(player.lwhealingspells) do
		
			local d = win.dataset[nr] or {}
			win.dataset[nr] = d
			
			d.id = spell.id
			d.label = spell.name
			d.value = spell.healing
			d.valuetext = Skada:FormatValueText(
											Skada:FormatNumber(spell.healing), self.metadata.columns.Healing,
											string.format("%02.1f%%", spell.overhealing / (spell.overhealing + spell.healing) * 100), self.metadata.columns.Overhealing,
											string.format("%02.1f%%", spell.healing / player.lwhealing * 100), self.metadata.columns.Percent
										)
			d.icon = select(3, GetSpellInfo(spell.id))
			
			if spell.healing > max then
				max = spell.healing
			end
			
			nr = nr + 1
		end
	end
	
	win.metadata.maxvalue = max
end

function mod:OnEnable()
	mod.metadata		= {showspots = true, click1 = spellsmod, columns = {Healing = true, Overhealing = true, Percent = true}}
	spellsmod.metadata	= {tooltip = spell_tooltip, columns = {Healing = true, Overhealing = true, Percent = true}}
	
	Skada:RegisterForCL(LWSpellHeal, 'SPELL_HEAL', {src_is_interesting = false, dst_is_not_interesting = false})
	Skada:RegisterForCL(LWSpellHeal, 'SPELL_PERIODIC_HEAL', {src_is_interesting = false, dst_is_not_interesting = false})

	Skada:AddMode(self)
end

function mod:OnDisable()
	Skada:RemoveMode(self)
end

function mod:GetSetSummary(set)
	return Skada:FormatNumber(set.lwhealing)
end

-- Called by Skada when a new player is added to a set.
function mod:AddPlayerAttributes(player)
	if not player.lwhealing then
		player.lwhealing = 0			-- Self Healing
		player.lwhealingspells = {}	-- Self Healing spells
		player.lwoverhealing = 0		-- Self Overheal total
	end
end

-- Called by Skada when a new set is created.
function mod:AddSetAttributes(set)
	if not set.lwhealing then
		set.lwhealing = 0
	end
end